package com.cskaoyan.service;


import com.cskaoyan.bean.bo.KeywordBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.KeywordPO;
import com.cskaoyan.bean.po.KeywordUpdatePO;
import com.cskaoyan.bean.vo.KeywordListVO;
import com.cskaoyan.bean.vo.KeywordVO;
import com.cskaoyan.mapper.KeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 * zx
 * 关键字
 */
@Service
public class KeywordServiceImpl implements KeywordService{

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public KeywordVO queryKeyword(BaseParam baseParam, String keyword, String url) {
        KeywordVO keywordVO = new KeywordVO();
//        keywordVO.setLimit(baseParam.getLimit());
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());

        List<KeywordPO> keywordPOS = keywordMapper.selectKeyword(baseParam.getSort(),baseParam.getOrder(),keyword,url);
        List<KeywordListVO> keywordListVOS = new ArrayList<>();
       //将数据放入list
        for (KeywordPO keywordPO : keywordPOS) {
            KeywordListVO keywordListVO = new KeywordListVO();
            keywordListVO.setId(keywordPO.getId());
            keywordListVO.setKeyword(keywordPO.getKeyword());
            keywordListVO.setUrl(keywordPO.getUrl());
            keywordListVO.setIsHot(keywordPO.getIs_hot());
            keywordListVO.setIsDefault(keywordPO.getIs_default());
            keywordListVO.setSortOrder(keywordPO.getSort_order());
            keywordListVOS.add(keywordListVO);
        }
        PageInfo pageInfo = new PageInfo(keywordPOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        keywordVO.setTotal((int) total);
        keywordVO.setPage(baseParam.getPage());
        keywordVO.setPages(pages);
        keywordVO.setLimit(baseParam.getLimit());
        keywordVO.setList(keywordListVOS);
        return keywordVO;
    }

    @Override
    public KeywordPO createNewKeyword(KeywordBO keywordBO) {
        KeywordPO keywordPO = new KeywordPO();
        keywordPO.setAdd_time(new Date());
        keywordPO.setId(null);
        keywordPO.setIs_default(keywordBO.getIsDefault());
        keywordPO.setIs_hot(keywordBO.getIsHot());
        keywordPO.setKeyword(keywordBO.getKeyword());
        keywordPO.setUpdate_time(new Date());
        keywordPO.setUrl(keywordBO.getUrl());
        int count = keywordMapper.insertKeyword(keywordPO);
        return keywordPO;
    }

    @Override
    public KeywordUpdatePO updateKeyword(KeywordBO keywordBO) {
        KeywordUpdatePO keywordPO = new KeywordUpdatePO();
        keywordPO.setAdd_time(keywordBO.getAddTime());
        keywordPO.setIs_default(keywordBO.getIsDefault());
        keywordPO.setIs_hot(keywordBO.getIsHot());
        keywordPO.setKeyword(keywordBO.getKeyword());
        keywordPO.setUpdate_time(new Date());
        keywordPO.setUrl(keywordBO.getUrl());
        keywordPO.setId(keywordBO.getId());
        int count = keywordMapper.updateKeyword(keywordPO);
        return keywordPO;
    }

    @Override
    public void delete(KeywordUpdatePO keywordUpdatePO) {
        int count = keywordMapper.deleteKeyword(keywordUpdatePO);
        return;
    }
}
